class mymodule {
	public $ci;
	public $data;
	public $path='admin/';
	public $a_moduleJs;
	public $a_moduleCss;
	public $szModuleName;
	
	public $szModulePath='';
	public function __construct($params)
    {
      
		
		$this->ci =& get_instance();
		$this->ci->load->model('db_model');
		$this->ci->load->library('session');
		$this->ci->load->helper('url');
		
		$this->szModuleName=strtolower($params['modulename']);
		$this->a_moduleJs=$params['a_moduleJs'];
		
		$this->a_moduleJs=array();
		$this->a_moduleCss=array();
		
		if($params['a_moduleJs'] )
		$this->a_moduleJs=$params['a_moduleJs'] ;
		
		if($params['a_moduleCss'] )
		$this->a_moduleCss=$params['a_moduleCss'] ;
		
		if($params['sz_ModulePath'] )
		$this->szModulePath=$params['sz_ModulePath'] ;
		else
		$this->szModulePath=base_url() . 'publics/uadmin/module/' . $this->szModuleName.'/' ;
		
		$this->checkPermission();
		check_login();
		
		 
	}
	public function module_js(){
		$js='<script>var base_url="'.base_url().'"</script>';
		$js.='<script src="'.base_url() . 'publics/uadmin/module/core/util.js"></script>';
		foreach($this->a_moduleJs as $sz_js){
			$js.='<script src="'.$this->szModulePath.$sz_js.'"></script>';
		}
		return $js;
	}
	public function module_css(){
		$css='<link rel="stylesheet" href="'.base_url() . 'publics/uadmin/css/icon.css" type="text/css" />';
		foreach($this->a_moduleCss as $sz_css){
			$css.='<link rel="stylesheet" href="'.$this->szModulePath.$sz_css.'" type="text/css" />';
		}
		return $css;
	}
	public function getMenu(){
		return $this->ci->db_model->getRows('*','sm_menus',' order by orders asc ');
	}
	public function getMenuByUser($i_UserId){
		return $this->ci->db_model->getRows('*','sm_usermenu','where user_id="'.$i_UserId.'" ');
	}
	public function viewMenu(){
		$sbadmin = $this->ci->session->userdata('sbadmin');
		// echo $sbadmin['id'].'t4s';
		// exit;
		$sidebar['menus'] = $this->getMenu();
		$sidebar['usermenu'] = $this->getMenuByUser($sbadmin['id']);
		// print_r($sidebar['usermenu']);
		// exit;
		$data['sidebar']=$sidebar;
		$data['adminpath']='administrator/';
		$this->ci->load->view($this->path.'sidebar',$data);
	}
	private function checkPermission(){
		$sbadmin = $this->ci->session->userdata('sbadmin');
		$menus =$this->getMenu();
		$usermenu = $this->getMenuByUser($sbadmin['id']);
		$current = $this->ci->uri->uri_string();
		$current=substr($current,1,strlen($current)-1);
		// echo $current;
		// exit;
		$b_permission=false;
		foreach($menus as $menu){
		 
		// echo $menu['link'].' = '.$current.'<br/>';
			$pos = strpos($current,$menu['link']);
			if (!($pos === false)) {
				
				foreach($usermenu as $um){
					if($um['menu_id']==$menu['id']){
						$b_permission=true;
					}
				}
			}
		}
		if(!$b_permission){
			echo'Access Deny';
			exit;
		}
		
	}
}
